#include "linkageTable.h"
#include "version.h"
#include "logger.h"
#include "db.h"
#include <string.h>
#include <stdlib.h>

/**
 *  
 */
#define LINKAGE_TABLE_NAME "t_linkage"
#define SQL_LINKAGE_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " LINKAGE_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, round INT NOT NULL, isRound INT NOT NULL, name TEXT NOT NULL, order INT, desc CHAR(256))"
#define SQL_LINKAGE_TABLE_INSERT(...) "INSERT INTO " LINKAGE_TABLE_NAME "(id, round, isRound, name, order, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_LINKAGE_TABLE_REPLACE(...) "REPLACE INTO " LINKAGE_TABLE_NAME "(id, round, isRound, name, order, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_LINKAGE_TABLE_DELETE(id) "DELETE FROM " LINKAGE_TABLE_NAME " WHERE id = " #id
#define SQL_LINKAGE_TABLE_SELECT(id) "SELECT * FROM " LINKAGE_TABLE_NAME " WHERE id = " #id



/*===============================================================*/
/* 联动 */
int createLinkageTable(){
    return db_create_table(SQL_LINKAGE_TABLE_CREATE);
}

int insertLinkage(T_LINKAGE* linkage){
    if(NULL == linkage){
        log_file_e(APPNAME, "insert linkage is NULL");
        return -1;
    }
    if((NULL == linkage->name) || (1 > strlen(linkage->name))){
        log_file_e(APPNAME, "insert linkage name is NULL or empty");
        return -1;
    }
    int ret = db_insert(SQL_LINKAGE_TABLE_INSERT(linkage->id, linkage->round, linkage->isRound, linkage->name, linkage->desc));
    return ret;
}

int replaceLinkage(T_LINKAGE* linkage){
    if(NULL == linkage){
        log_file_e(APPNAME, "replace linkage is NULL");
        return -1;
    }
    if((NULL == linkage->name) || (1 > strlen(linkage->name))){
        log_file_e(APPNAME, "replace linkage name is NULL or empty");
        return -1;
    }
    int ret = db_insert(SQL_LINKAGE_TABLE_REPLACE(linkage->id, linkage->round, linkage->isRound, linkage->name, linkage->desc));
    return ret;
}

int deleteLinkage(T_LINKAGE* linkage){
    if(NULL == linkage){
        log_file_e(APPNAME, "delete linkage is NULL");
        return -1;
    }
    return db_delete(SQL_LINKAGE_TABLE_DELETE(linkage->id));
}
/**
 *  
 */
static int linkageCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "linkageCallback data is NULL");
        return -1;
    }
    T_LINKAGE_LIST* linkageList = (T_LINKAGE_LIST*)data;
    if((0 == linkageList->count) || (NULL == linkageList->head)){
        linkageList->head = linkageList->tail = (T_LINKAGE*)malloc(sizeof(T_LINKAGE));
        linkageList->head->next = NULL;
        linkageList->head->prev = NULL;
    } else {
        linkageList->tail->next = (T_LINKAGE*)malloc(sizeof(T_LINKAGE));
        linkageList->tail->next->prev = linkageList->tail;
        linkageList->tail = linkageList->tail->next;
    }
    linkageList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            linkageList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "round")){
            linkageList->tail->round = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "isRound")){
            linkageList->tail->isRound = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "name")){
            if(NULL == argv[i]){
                linkageList->tail->name[0] = '\0';
            } else {
                strcpy(linkageList->tail->name, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "order")){
            linkageList->tail->order = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "desc")){
            if(NULL == argv[i]){
                linkageList->tail->desc[0] = '\0';
            } else {
                strcpy(linkageList->tail->desc, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return linkageList->count;
}

int selectLinkage(const char* sql, T_LINKAGE_LIST* linkageList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == linkageList)){
        log_file_e(APPNAME, "select product sql is NULL");
        return -1;
    }
    return db_select(sql, linkageCallback, linkageList);

}
//
void dbLinkageInit(){
    createLinkageTable();
    createLinkageConditionTable();
    createLinkageActionTable();
}
